package van.planifolia.work;

import java.util.concurrent.CopyOnWriteArrayList;

/**
 * Created by Intellij IDEA<br>
 *
 * @author Planifolia.Van
 * @version 1.0
 * @date 2022/11/2 18:27
 */
public class SafeCollection {
    public static void main(String[] args) {
        // 创建线程安全的List
        CopyOnWriteArrayList<String> safeList = new CopyOnWriteArrayList<>();
        // 创建20个线程来进行读写操作，最后发现并没有任何的线程安全错误
        for (int i = 0; i < 20; i++) {
            new Thread(()->{
                safeList.add(Thread.currentThread().getName());
                System.out.println(safeList);
            }).start();
        }

    }
}
